xxx= '''
[
    {
        "name": "ROC_CRITICAL",
        "defined_by_vo_name": "ops",
        "groups": [
            {
                "name": "INFN-BOLOGNA-T3",
                "type": "Site",
                "availabilities": [
                    {
                        "timestamp": "2011-12-01T00:00:00Z",
                        "availability": "0.940302958963",
                        "reliability": "0.940302958963",
                        "maintenance": "0.0"
                    }
                    
                ]
            },
'''

import json

fileROC= "ROC.json"
fileWLCG= "WLCG.json"
A = "ROC_CRITICAL"
B = "WLCG_CREAM_LCGCE_CRITICAL"

sites_AR = {}

#Read files
rfile = open (fileROC, "r")
jsonROC = json.loads(rfile.read())
rfile.close()

rfile = open (fileWLCG, "r")
jsonWLCG = json.loads(rfile.read())
rfile.close()




for site_n in jsonROC[0]['groups']:
    sites_AR[site_n['name']] = {'ROC':(site_n['availabilities'][0]['availability'],site_n['availabilities'][0]['reliability']),'WLCG':('-1','-1')}


for site_n in jsonWLCG[0]['groups']:
    dictionaryTmp =  {'WLCG':(site_n['availabilities'][0]['availability'],site_n['availabilities'][0]['reliability'])}
    if site_n['name'] in sites_AR.keys():
        sites_AR[site_n['name']].update(dictionaryTmp)
    else:
        sites_AR[site_n['name']] = dictionaryTmp
        sites_AR[site_n['name']]['ROC']=('-1','-1') #Add unknown if there is no ROC values
        


print "Site Name, ROC_CRITICAL A, ROC_CRITICAL R, WLCG_A, WLCG_R"
for sitename in sites_AR.keys():
    print sitename+",",
    print "%s, %s, %s,%s "%(sites_AR[sitename]['ROC'][0],sites_AR[sitename]['ROC'][1],sites_AR[sitename]['WLCG'][0],sites_AR[sitename]['WLCG'][1])
    
    


